﻿Imports System.Data.SqlClient
Public Class BooksByLanguage
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Check if the URL contains the required parameter ("CodingLanguage_FK")
        Dim checkLanguage As Integer = Request.Params("CodingLanguage_FK")
        Dim languageName As String = ""
        If checkLanguage <= 0 Then
            Response.Redirect("Books.aspx")
        End If

        'Retrieve the name of the coding language
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim SqlString As String = "SELECT * FROM CodingLanguages WHERE ID = @f1"
        Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("CodingLanguage_FK"))
        'Getting records (only 1 line for selected Bibliophile)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("ID")) Then
                languageName = dr("Language")
            End If
        End If
        'Assign text to the heading
        myHeading.InnerHtml = "Books with " & languageName
        If IsPostBack = False Then
            'Populate the tag cloud
            TagCloud.getTagCloud(SqlDataSource1, CloudTags, Page)
            LanguageCloud.getLanguageCloud(SqlDataSource3, LanguageTags, Page)
        End If
    End Sub

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If datasource is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Books found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBooks.Visible = False
        Else
            DataPagerBooks.Visible = True
        End If
    End Sub
    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBooks.Visible = (DataPagerBooks.PageSize < DataPagerBooks.TotalRowCount)
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BookName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByCategory.Click
        searchDatabase("Category", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        Dim checkLanguage As Integer = Request.Params("CodingLanguage_FK")
        SqlDataSource2.SelectCommand = "SELECT * FROM Books_Complete WHERE " & buttonStr & " LIKE '%" & searchStr & "%' AND (CodingLanguage_FK = " & checkLanguage & ") AND IsVerified = 1 ORDER BY DateAdded DESC"
        SqlDataSource2.DataBind()
        Return True
    End Function

End Class